package com.yc.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.service.UmsAdminService;
import com.yc.cloud.admin.service.UmsResourceCacheService;
import com.yc.cloud.admin.service.UmsRoleService;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.entity.UmsAdminRoleRelation;
import com.yc.cloud.entity.UmsRole;
import com.yc.cloud.entity.UmsRoleMenuRelation;
import com.yc.cloud.entity.UmsRoleResourceRelation;
import com.yc.cloud.mapper.UmsAdminRoleRelationMapper;
import com.yc.cloud.mapper.UmsRoleMapper;
import com.yc.cloud.mapper.UmsRoleMenuRelationMapper;
import com.yc.cloud.mapper.UmsRoleResourceRelationMapper;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author JackGu
 * @since 2022-09-11
 */
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements UmsRoleService {
    @Resource
    private UmsResourceCacheService umsResourceCacheService;
    @Resource
    private UmsAdminService umsAdminService;


    @Resource
    private UmsRoleMapper umsRoleMapper;
    @Resource
    private UmsAdminRoleRelationMapper adminRoleRelationMapper;
    @Resource
    private UmsRoleMenuRelationMapper roleMenuRelationMapper;
    @Resource
    private UmsRoleResourceRelationMapper roleResourceRelationMapper;

    @Override
    public IPage<UmsRole> list(BasePageReqDTO queryParam) {
        val query = new QueryWrapper<UmsRole>();
        IPage<UmsRole> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BasePageReqDTO.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(UmsRole.ID);
            } else if (BasePageReqDTO.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(UmsRole.ID);
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {

            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), umsRoleMapper.selectCount(query), true);
        }
        return umsRoleMapper.selectPage(page, query);
    }

    @Override
    public List<UmsRole> getRoleList(Long adminId) {
        return umsRoleMapper.getRoleList(adminId);
    }

    @Override
    public int updateRole(Long adminId, List<Long> roleIds) {
        int count = roleIds == null ? 0 : roleIds.size();
        //先删除原来的关系
        val query = new QueryWrapper<UmsAdminRoleRelation>();
        adminRoleRelationMapper.delete(query.eq("admin_id", adminId));
        //建立新关系
        if (!CollectionUtils.isEmpty(roleIds)) {
            // List<UmsAdminRoleRelation> list = new ArrayList<>();
            for (Long roleId : roleIds) {
                UmsAdminRoleRelation roleRelation = new UmsAdminRoleRelation();
                roleRelation.setAdminId(adminId);
                roleRelation.setRoleId(roleId);
                // list.add(roleRelation);
                adminRoleRelationMapper.insert(roleRelation);
            }
        }
        val umsAdmin = umsAdminService.getById(adminId);
        if (umsAdmin != null) {
            umsResourceCacheService.delByUserName(umsAdmin.getUsername());
        }
        return count;
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        val query = new QueryWrapper<UmsRoleMenuRelation>();
        query.eq(UmsRoleMenuRelation.ROLE_ID, roleId);
        roleMenuRelationMapper.delete(query);

        //批量插入新关系
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuRelationMapper.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        val query = new QueryWrapper<UmsRoleResourceRelation>();
        query.eq("role_id", roleId);
        roleResourceRelationMapper.delete(query);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceRelationMapper.insert(relation);
        }
        // umsResourceCacheService.delResourceListByRole(roleId);
        return resourceIds.size();
    }
}
